//$(document).ready(function() {
var admin_SystemStatus = function() {
    var monitorid, type, time, periods, vmMonitorStatus, hostMonitorStatus;
    var cpu_all = [],
    cpu_user = [],
    cpu_system = [],
    cpu_iowait = [],
    memory = [],
    network_in = [],
    network_out = [],
    dom0_cpu_user = [],
    dom0_cpu_system = [],
    dom0_cpu_iowait = [];
    function getAdminMonitorHosts(callback) {
	var j ;
        for (j = 0; j < allsites.length; ++j) { (function(siteName) {
                $.get('/' + siteName + '/admin/execute_wait_cacheable/iaas.get_hostpool_info.json', {},
                function(serverResponse) {
                    var hostname;

                    var hostid;

                    var cpus;

                    var cpuused;

                    var mem;

                    var freemem;

                    var hostedvm;
                    var hostData = [];
                    if (serverResponse[0] === false) {
                        return;
                    }
                    for (var i = 0; i < serverResponse[1].length; i++) {
                        hostname = serverResponse[1][i].name;

                        hostid = serverResponse[1][i].guid;

                        cpus = serverResponse[1][i].totalCpu / 100;

                        cpuused = serverResponse[1][i].usedCpu;

                        mem = serverResponse[1][i].totalMemory / 1024;

                        freemem = serverResponse[1][i].freeMemory / 1024;
                        hostedvm = serverResponse[1][i].virtualMachines;
                        hostData.push([siteName, hostname, hostid, cpus, mem, hostedvm]);
                    }
                    callback(siteName, hostData);
                },
                'json');
            })(allsites[j]);
        }
    };
    function loadadminMonitorHosts(siteName, hostData) {
        for (var j = $('#adminSystemStatusHostTable').dataTable().fnSettings().aoData.length - 1; j >= 0; j--) {
            var name = $('#adminSystemStatusHostTable').dataTable().fnSettings().aoData[j]._aData[0];
            if (name == siteName) {
                var node = $('#adminSystemStatusHostTable').dataTable().fnGetNodes(j);
                $('#adminSystemStatusHostTable').dataTable().fnDeleteRow(node);
            }
        }
        $('#adminSystemStatusHostTable').dataTable().fnAddData(hostData);
    };
    function getAdminMonitorVMs(callback) {
        for (var j = 0; j < allsites.length; ++j) { (function(siteName) {
                $.get('/' + siteName + '/admin/execute_wait_cacheable/iaas.get_vms_info.json', {},
                function(serverResponse) {
                    var hostMachine;
                    var vmid;
                    var vmip;
                    var vmport;
                    var vmname;
                    var vcpus;
                    var memorysize;
                    var stat;
                    var owner;
                    var starttime;
                    var tableData = [];
                    if (serverResponse[0] === false) {
                        return;
                    }
                    for (var i = 0; i < serverResponse[1].length; i++) {

                        vmid = serverResponse[1][i].guid;

                        vmname = serverResponse[1][i].name;

                        vmip = serverResponse[1][i].ip;
                        vmport = serverResponse[1][i].port;
                        vcpus = serverResponse[1][i].cpu;

                        memorysize = serverResponse[1][i].memory; //1024;
                        hostMachine = serverResponse[1][i].hostMachine;
                        owner = serverResponse[1][i].owner;

                        stat = serverResponse[1][i].stat;
                        starttime = serverResponse[1][i].stime;
                        tableData.push([vmid, siteName, hostMachine, vcpus, memorysize, stat]);
                        /*$('#adminSystemStatusVMTable').dataTable().fnAddData([
					vmid,
					siteName,
					hostMachine,
					vcpus,
					memory,
					stat,
					]);*/
                    }
                    callback(siteName, tableData);
                },
                'json');
            })(allsites[j]);
        }
    };
    function loadadminMonitorVMs(siteName, tableData) {
        for (var j = $('#adminSystemStatusVMTable').dataTable().fnSettings().aoData.length - 1; j >= 0; j--) {
            var name = $('#adminSystemStatusVMTable').dataTable().fnSettings().aoData[j]._aData[1];
            if (name == siteName) {
                var node = $('#adminSystemStatusVMTable').dataTable().fnGetNodes(j);
                $('#adminSystemStatusVMTable').dataTable().fnDeleteRow(node);
            }
        }
        $('#adminSystemStatusVMTable').dataTable().fnAddData(tableData);
    };
    function getCpuInfoHistory(siteName, monitorid, type, periods, callback) {
        cpu_all = [];
        cpu_user = [];
        cpu_system = [];
        cpu_iowait = [];
        $.get('/' + siteName + '/admin/execute_wait_cacheable/mon_getter.get_stats.json?hostId=' + monitorid + "&metricName=cpu_user&stat=" + type + "&startTime=" + periods, {},
        function(cpu_user_time) {
            cpu_user = cpu_user_time;
            $.get('/' + siteName + '/admin/execute_wait_cacheable/mon_getter.get_stats.json?hostId=' + monitorid + "&metricName=cpu_system&stat=" + type + "&startTime=" + periods, {},
            function(cpu_system_time) {
                cpu_system = cpu_system_time;
                $.get('/' + siteName + '/admin/execute_wait_cacheable/mon_getter.get_stats.json?hostId=' + monitorid + "&metricName=cpu_iowait&stat=" + type + "&startTime=" + periods, {},
                function(cpu_iowait_time) {
                    cpu_iowait = cpu_iowait_time;
                    $.get('/' + siteName + '/admin/execute_wait_cacheable/mon_getter.get_stats.json?hostId=' + monitorid + "&metricName=cpu_usage&stat=" + type + "&startTime=" + periods, {},
                    function(cpu_usage_time) {
                        cpu_all = cpu_usage_time;
                        callback(cpu_all, cpu_user, cpu_system, cpu_iowait);
                    },
                    'json');
                },
                'json');
            },
            'json');
        },
        'json');
    };
    function getCpuInfoStatus(siteName, monitorid, type, cpu_all, cpu_user, cpu_system, cpu_iowait, callback) {
        $.get('/' + siteName + '/admin/execute_wait_cacheable/mon_getter.get_stats.json?hostId=' + monitorid + "&metricName=cpu_user&stat=" + type, {},
        function(cpu_user_time) {
            if (cpu_user_time) {
                if (cpu_user[cpu_user.length - 1][0] < cpu_user_time[0][0]) {
                    cpu_user.push(cpu_user_time[0]);
                    if (cpu_user.length > 100) {
                        cpu_user.shift();
                    }
                }
            }
            $.get('/' + siteName + '/admin/execute_wait_cacheable/mon_getter.get_stats.json?hostId=' + monitorid + "&metricName=cpu_system&stat=" + type, {},
            function(cpu_system_time) {
                if (cpu_system_time) {
                    if (cpu_system[cpu_system.length - 1][0] < cpu_system_time[0][0]) {
                        cpu_system.push(cpu_system_time[0]);
                        if (cpu_system.length > 100) {
                            cpu_system.shift();
                        }
                    }
                }
                $.get('/' + siteName + '/admin/execute_wait_cacheable/mon_getter.get_stats.json?hostId=' + monitorid + "&metricName=cpu_iowait&stat=" + type, {},
                function(cpu_iowait_time) {
                    if (cpu_iowait_time) {
                        if (cpu_iowait[cpu_iowait.length - 1][0] < cpu_iowait_time[0][0]) {
                            cpu_iowait.push(cpu_iowait_time[0]);
                            if (cpu_iowait.length > 100) {
                                cpu_iowait.shift();
                            }
                        }
                    }
                    $.get('/' + siteName + '/admin/execute_wait_cacheable/mon_getter.get_stats.json?hostId=' + monitorid + "&metricName=cpu_usage&stat=" + type, {},
                    function(cpu_usage_time) {
                        if (cpu_usage_time) {
                            if (cpu_all[cpu_all.length - 1][0] < cpu_usage_time[0][0]) {
                                cpu_all.push(cpu_usage_time[0]);
                                if (cpu_all.length > 100) {
                                    cpu_all.shift();
                                }
                            }
                        }
                        callback(cpu_all, cpu_user, cpu_system, cpu_iowait);
                    },
                    'json');
                },
                'json');
            },
            'json');
        },
        'json');
    };
    function drawCpuHistory(cpu_all, cpu_user, cpu_system, cpu_iowait) {
        var CpuOptions = {
            xaxis: {
                tickFormatter: function(val, axis) {
                    return new Date(val * 1000).toTimeString().split(' ')[0];
                }
            },
            yaxis: {
                tickFormatter: function(val, axis) {
                    return val.toFixed(axis.tickDecimals) + '%';
                }
            }
        };
        if (($("#adminSystemStatusCPUGraph").width() !== 0) && ($("#adminSystemStatusCPUGraph").height() !== 0)) {
            $.plot($("#adminSystemStatusCPUGraph"), [{
                label: "cpu_all",
                data: cpu_all
            },
            {
                label: "cpu_user",
                data: cpu_user
            },
            {
                label: "cpu_system",
                data: cpu_system
            },
            {
                label: "cpu_iowait",
                data: cpu_iowait
            }], CpuOptions);
        }
    };
    function getMemInfoHistory(siteName, monitorid, type, periods, callback) {
        $.get('/' + siteName + '/admin/execute_wait_cacheable/mon_getter.get_stats.json?hostId=' + monitorid + '&metricName=mem_usage&stat=' + type + '&startTime=' + periods, {},
        function(serverResponse) {
            memory = serverResponse;
            callback(serverResponse);
        },
        'json');
    };
    function getMemInfoStatus(siteName, monitorid, type, memory, callback) {
        $.get('/' + siteName + '/admin/execute_wait_cacheable/mon_getter.get_stats.json?hostId=' + monitorid + "&metricName=mem_usage&stat=" + type, {},
        function(serverResponse) {
            if (serverResponse) {
                if (memory[memory.length - 1][0] < serverResponse[0][0]) {
                    memory.push(serverResponse[0]);
                    if (memory.length > 100) {
                        memory.shift();
                    }
                }
            }
            callback(memory);
        },
        'json');
    };
    function drawMemHistory(serverResponse) {
        var MemOptions = {
            xaxis: {
                tickFormatter: function(val, axis) {
                    return new Date(val * 1000).toTimeString().split(' ')[0];
                }
            },
            yaxis: {
                tickFormatter: function(val, axis) {
                    return val.toFixed(axis.tickDecimals) + '%';
                }
            }
        };
        if (($("#adminSystemStatusMemoryGraph").width() !== 0) && ($("#adminSystemStatusMemoryGraph").height() !== 0)) {
            $.plot($("#adminSystemStatusMemoryGraph"), [{
                label: "memory",
                data: serverResponse
            }], MemOptions);
        }
    };
    function getNetInInfoHistory(siteName, monitorid, type, periods, callback) {
        $.get('/' + siteName + '/admin/execute_wait_cacheable/mon_getter.get_stats.json?hostId=' + monitorid + '&metricName=bytes_in&stat=' + type + '&startTime=' + periods, {},
        function(serverResponse) {
            network_in = serverResponse;
            $.get('/' + siteName + '/admin/execute_wait_cacheable/mon_getter.get_stats.json?hostId=' + monitorid + '&metricName=bytes_out&stat=' + type + '&startTime=' + periods, {},
            function(serverResponse) {
                network_out = serverResponse;
                callback(network_in, network_out);
            },
            'json');
        },
        'json');
    };
    function getNetInInfoStatus(siteName, monitorid, type, network_in, network_out, callback) {
        $.get('/' + siteName + '/admin/execute_wait_cacheable/mon_getter.get_stats.json?hostId=' + monitorid + "&metricName=bytes_in&stat=" + type, {},
        function(serverResponse) {
            if (serverResponse) {
                if (network_in[network_in.length - 1][0] < serverResponse[0][0]) {
                    network_in.push(serverResponse[0]);
                    if (network_in.length > 100) {
                        network_in.shift();
                    }
                }
            }
            $.get('/' + siteName + '/admin/execute_wait_cacheable/mon_getter.get_stats.json?hostId=' + monitorid + "&metricName=bytes_out&stat=" + type, {},
            function(serverResponse) {
                if (serverResponse) {
                    if (network_out[network_out.length - 1][0] < serverResponse[0][0]) {
                        network_out.push(serverResponse[0]);
                        if (network_out.length > 100) {
                            network_out.shift();
                        }
                    }
                }
                callback(network_in, network_out);
            },
            'json');
        },
        'json');
    };
    function drawNetInHistory(network_in, network_out) {
        var NetworkOptions = {
            xaxis: {
                tickFormatter: function(val, axis) {
                    return new Date(val * 1000).toTimeString().split(' ')[0];
                }
            },
            yaxis: {
                tickFormatter: function(val, axis) {
                    if (val > 1048576) return (val / 1048576).toFixed(axis.tickDecimals) + 'MB/s';
                    else if (val > 1024) return (val / 1024).toFixed(axis.tickDecimals) + 'KB/s';
                    else return val.toFixed(axis.tickDecimals) + 'B/s';
                }
            }
        };
        if (($("#adminSystemStatusNetworkGraph").width() !== 0) && ($("#adminSystemStatusNetworkGraph").height() !== 0)) {
            $.plot($("#adminSystemStatusNetworkGraph"), [{
                label: "network_in",
                data: network_in
            },
            {
                label: "network_out",
                data: network_out
            }], NetworkOptions);
        }
    };
    /*function getNetOutInfoHistory(siteName,monitorid,type,periods,callback){
        $.get('/'+siteName+'/admin/execute_wait_cacheable/mon_getter.get_stats.json?hostId='+monitorid+'&metricName=bytes_out&stat='+type+'&startTime='+periods,{},
        function(serverResponse) {
		network_out=serverResponse;
		callback(serverResponse);
        }, 'json');
};
function getNetOutInfoStatus(siteName,monitorid,type,network_out,callback){
	$.get('/'+siteName+'/admin/execute_wait_cacheable/mon_getter.get_stats.json?hostId='+monitorid+"&metricName=bytes_out&stat="+type,{},
	function(serverResponse) {
	if (serverResponse) 
	{
		if(network_out[network_out.length-1][0]<serverResponse[0][0])		{network_out.push(serverResponse[0]);if(network_out.length>100) {network_out.shift();}}
	}
        callback(network_out);
	},'json');
};
function drawNetOutHistory(serverResponse){
	var NetworkOptions = {
                xaxis: {tickFormatter: function (val, axis) {
			return new Date(val*1000).toTimeString().split(' ')[0];
		}},
                yaxis: {
                    tickFormatter: function(val, axis) {
                        if (val > 1048576) return (val / 1048576).toFixed(axis.tickDecimals) + 'MB/s';
                        else if (val > 1024) return (val / 1024).toFixed(axis.tickDecimals) + 'KB/s';
                        else return val.toFixed(axis.tickDecimals) + 'B/s';
                    }
                }
            };
	if (($("#adminSystemStatusDiskGraph").width() != 0) && ($("#adminSystemStatusDiskGraph").height() != 0)) 
	{
		$.plot($("#adminSystemStatusDiskGraph"), [{label: "network_out",data: serverResponse}], NetworkOptions);
	}
};*/
    function plotAdminSystemVMStatusGraphHistory() {
        if (($("#adminSystemStatusCPUGraph").width() !== 0) && ($("#adminSystemStatusCPUGraph").height() !== 0)) {
            $.plot($("#adminSystemStatusCPUGraph"), [{
                label: "cpu",
                data: [[0, 0]]
            }]);
        }
        if (($("#adminSystemStatusMemoryGraph").width() !== 0) && ($("#adminSystemStatusMemoryGraph").height() !== 0)) {
            $.plot($("#adminSystemStatusMemoryGraph"), [{
                label: "memory",
                data: [[0, 0]]
            }]);
        }
        if (($("#adminSystemStatusNetworkGraph").width() !== 0) && ($("#adminSystemStatusNetworkGraph").height() !== 0)) {
            $.plot($("#adminSystemStatusNetworkGraph"), [{
                label: "network_in",
                data: [[0, 0]]
            }]);
        }
        /*if (($("#adminSystemStatusDiskGraph").width() != 0) && ($("#adminSystemStatusDiskGraph").height() != 0)) {$.plot($("#adminSystemStatusDiskGraph"), [{
            label: "network_out",
            data: [[0, 0]]
        }]);}*/
        var selectedVM = $('td', fnGetSelected(adminSystemStatusVMTable));
        var selected;
        var siteName;
        selected = selectedVM[0];
        monitorid = $(selected).text();
        siteName = $(selectedVM[1]).text();
        if (periods == '15min') {
            periods = -900;
        }
        if (periods == '30min') {
            periods = -1800;
        }
        if (periods == '1h') {
            periods = -3600;
        }
        if (periods == '3h') {
            periods = -10800;
        }
        if (periods == '6h') {
            periods = -21600;
        }
        getCpuInfoHistory(siteName, monitorid, type, periods, drawCpuHistory);
        getMemInfoHistory(siteName, monitorid, type, periods, drawMemHistory);
        getNetInInfoHistory(siteName, monitorid, type, periods, drawNetInHistory);
        //getNetOutInfoHistory(siteName,monitorid,type,periods,drawNetOutHistory);
    };
    function getHostCpuInfoHistory(siteName, monitorid, type, periods, callback) {
        cpu_all = [];
        dom0_cpu_user = [];
        dom0_cpu_system = [];
        dom0_cpu_iowait = [];
        $.get('/' + siteName + '/admin/execute_wait_cacheable/mon_getter.get_stats.json?hostId=' + monitorid + '&metricName=dom0_cpu_user&stat=' + type + '&startTime=' + periods, {},
        function(dom0_cpu_user_time) {
            dom0_cpu_user = dom0_cpu_user_time;
            $.get('/' + siteName + '/admin/execute_wait_cacheable/mon_getter.get_stats.json?hostId=' + monitorid + '&metricName=dom0_cpu_system&stat=' + type + '&startTime=' + periods, {},
            function(dom0_cpu_system_time) {
                dom0_cpu_system = dom0_cpu_system_time;
                $.get('/' + siteName + '/admin/execute_wait_cacheable/mon_getter.get_stats.json?hostId=' + monitorid + '&metricName=dom0_cpu_iowait&stat=' + type + '&startTime=' + periods, {},
                function(dom0_cpu_iowait_time) {
                    dom0_cpu_iowait = dom0_cpu_iowait_time;
                    $.get('/' + siteName + '/admin/execute_wait_cacheable/mon_getter.get_stats.json?hostId=' + monitorid + '&metricName=cpu_usage&stat=' + type + '&startTime=' + periods, {},
                    function(cpu_usage_time) {
                        cpu_all = cpu_usage_time;
                        callback(cpu_all, dom0_cpu_user, dom0_cpu_system, dom0_cpu_iowait);

                    },
                    'json');
                },
                'json');
            },
            'json');
        },
        'json');
    };
    function getHostCpuInfoStatus(siteName, monitorid, type, cpu_all, dom0_cpu_user, dom0_cpu_system, dom0_cpu_iowait, callback) {
        $.get('/' + siteName + '/admin/execute_wait_cacheable/mon_getter.get_stats.json?hostId=' + monitorid + '&metricName=dom0_cpu_user&stat=' + type, {},
        function(dom0_cpu_user_time) {
            if (dom0_cpu_user_time) {
                if (dom0_cpu_user[dom0_cpu_user.length - 1][0] < dom0_cpu_user_time[0][0]) {
                    dom0_cpu_user.push(dom0_cpu_user_time[0]);
                    if (dom0_cpu_user.length > 100) {
                        dom0_cpu_user.shift();
                    }
                }
            }
            $.get('/' + siteName + '/admin/execute_wait_cacheable/mon_getter.get_stats.json?hostId=' + monitorid + '&metricName=dom0_cpu_system&stat=' + type, {},
            function(dom0_cpu_system_time) {
                if (dom0_cpu_system_time) {
                    if (dom0_cpu_system[dom0_cpu_system.length - 1][0] < dom0_cpu_system_time[0][0]) {
                        dom0_cpu_system.push(dom0_cpu_system_time[0]);
                        if (dom0_cpu_system.length > 100) {
                            dom0_cpu_system.shift();
                        }
                    }
                }
                $.get('/' + siteName + '/admin/execute_wait_cacheable/mon_getter.get_stats.json?hostId=' + monitorid + '&metricName=dom0_cpu_iowait&stat=' + type, {},
                function(dom0_cpu_iowait_time) {
                    if (dom0_cpu_iowait_time) {
                        if (dom0_cpu_iowait[dom0_cpu_iowait.length - 1][0] < dom0_cpu_iowait_time[0][0]) {
                            dom0_cpu_iowait.push(dom0_cpu_iowait_time[0]);
                            if (dom0_cpu_iowait.length > 100) {
                                dom0_cpu_iowait.shift();
                            }
                        }
                    }
                    $.get('/' + siteName + '/admin/execute_wait_cacheable/mon_getter.get_stats.json?hostId=' + monitorid + '&metricName=cpu_usage&stat=' + type, {},
                    function(cpu_usage_time) {
                        if (cpu_usage_time) {
                            if (cpu_all[cpu_all.length - 1][0] < cpu_usage_time[0][0]) {
                                cpu_all.push(cpu_usage_time[0]);
                                if (cpu_all.length > 100) {
                                    cpu_all.shift();
                                }
                            }
                        }
                        callback(cpu_all, dom0_cpu_user, dom0_cpu_system, dom0_cpu_iowait);
                    },
                    'json');
                },
                'json');
            },
            'json');
        },
        'json');
    };
    function drawHostCpuHistory(cpu_all, dom0_cpu_user, dom0_cpu_system, dom0_cpu_iowait) {
        var CpuOptions = {
            xaxis: {
                tickFormatter: function(val, axis) {
                    return new Date(val * 1000).toTimeString().split(' ')[0];
                }
            },
            yaxis: {
                tickFormatter: function(val, axis) {
                    return val.toFixed(axis.tickDecimals) + '%';
                }
            }
        };
        if (($("#adminSystemStatusCPUGraph").width() !== 0) && ($("#adminSystemStatusCPUGraph").height() !== 0)) {
            $.plot($("#adminSystemStatusCPUGraph"), [{
                label: "cpu_all",
                data: cpu_all
            },
            {
                label: "dom0_cpu_user",
                data: dom0_cpu_user
            },
            {
                label: "dom0_cpu_system",
                data: dom0_cpu_system
            },
            {
                label: "dom0_cpu_iowait",
                data: dom0_cpu_iowait
            }], CpuOptions);
        }
    };
    function plotAdminSystemHostStatusGraphHistory() {
        if (($("#adminSystemStatusCPUGraph").width() !== 0) && ($("#adminSystemStatusCPUGraph").height() !== 0)) {
            $.plot($("#adminSystemStatusCPUGraph"), [{
                label: "cpu",
                data: [[0, 0]]
            }]);
        }
        if (($("#adminSystemStatusMemoryGraph").width() !== 0) && ($("#adminSystemStatusMemoryGraph").height() !== 0)) {
            $.plot($("#adminSystemStatusMemoryGraph"), [{
                label: "memory",
                data: [[0, 0]]
            }]);
        }
        if (($("#adminSystemStatusNetworkGraph").width() !== 0) && ($("#adminSystemStatusNetworkGraph").height() !== 0)) {
            $.plot($("#adminSystemStatusNetworkGraph"), [{
                label: "network_in",
                data: [[0, 0]]
            }]);
        }
        /*if (($("#adminSystemStatusDiskGraph").width() != 0) && ($("#adminSystemStatusDiskGraph").height() != 0)) {$.plot($("#adminSystemStatusDiskGraph"), [{
            label: "network_out",
            data: [[0, 0]]
        }]);}*/
        var selectedHost = $('td', fnGetSelected(adminSystemStatusHostTable));
        var selected;
        var siteName;
        selected = selectedHost[1];
        monitorid = $(selected).text();
        siteName = $(selectedHost[0]).text();
        if (periods == '15min') {
            periods = -900;
        }
        if (periods == '30min') {
            periods = -1800;
        }
        if (periods == '1h') {
            periods = -3600;
        }
        if (periods == '3h') {
            periods = -10800;
        }
        if (periods == '6h') {
            periods = -21600;
        }
        getHostCpuInfoHistory(siteName, monitorid, type, periods, drawHostCpuHistory);
        getMemInfoHistory(siteName, monitorid, type, periods, drawMemHistory);
        getNetInInfoHistory(siteName, monitorid, type, periods, drawNetInHistory);
        //getNetOutInfoHistory(siteName,monitorid,type,periods,drawNetOutHistory);
    };
    function plotAdminSystemVMStatusGraph() {
        if (($("#adminSystemStatusCPUGraph").width() !== 0) && ($("#adminSystemStatusCPUGraph").height() !== 0)) {
            $.plot($("#adminSystemStatusCPUGraph"), [{
                label: "cpu",
                data: [[0, 0]]
            }]);
        }
        if (($("#adminSystemStatusMemoryGraph").width() !== 0) && ($("#adminSystemStatusMemoryGraph").height() !== 0)) {
            $.plot($("#adminSystemStatusMemoryGraph"), [{
                label: "memory",
                data: [[0, 0]]
            }]);
        }
        if (($("#adminSystemStatusNetworkGraph").width() !== 0) && ($("#adminSystemStatusNetworkGraph").height() !== 0)) {
            $.plot($("#adminSystemStatusNetworkGraph"), [{
                label: "network_in",
                data: [[0, 0]]
            }]);
        }
        /*if (($("#adminSystemStatusDiskGraph").width() != 0) && ($("#adminSystemStatusDiskGraph").height() != 0)) {$.plot($("#adminSystemStatusDiskGraph"), [{
            label: "network_out",
            data: [[0, 0]]
        }]);}*/
        var selectedVM = $('td', fnGetSelected(adminSystemStatusVMTable));
        var selected;
        var siteName;
        siteName = $(selectedVM[1]).text();
        selected = selectedVM[0];
        monitorid = $(selected).text();
        if (time == '15sec') {
            time = 15000;
        }
        if (time == '1min') {
            time = 60000;
        }
        if (time == '5min') {
            time = 300000;
        }
        var pretime = -1500;
        getCpuInfoHistory(siteName, monitorid, type, pretime, drawCpuHistory);
        getMemInfoHistory(siteName, monitorid, type, pretime, drawMemHistory);
        getNetInInfoHistory(siteName, monitorid, type, pretime, drawNetInHistory);
        //getNetOutInfoHistory(siteName,monitorid,type,pretime,drawNetOutHistory);
        function fetchData() {
            getCpuInfoStatus(siteName, monitorid, type, cpu_all, cpu_user, cpu_system, cpu_iowait, drawCpuHistory);
            getMemInfoStatus(siteName, monitorid, type, memory, drawMemHistory);
            getNetInInfoStatus(siteName, monitorid, type, network_in, network_out, drawNetInHistory);
            //getNetOutInfoStatus(siteName,monitorid,type,network_out,drawNetOutHistory);       
        }
        vmMonitorStatus = setInterval(fetchData, time);
    };
    function plotAdminSystemHostStatusGraph() {
        if (($("#adminSystemStatusCPUGraph").width() !== 0) && ($("#adminSystemStatusCPUGraph").height() !== 0)) {
            $.plot($("#adminSystemStatusCPUGraph"), [{
                label: "cpu",
                data: [[0, 0]]
            }]);
        }
        if (($("#adminSystemStatusMemoryGraph").width() !== 0) && ($("#adminSystemStatusMemoryGraph").height() !== 0)) {
            $.plot($("#adminSystemStatusMemoryGraph"), [{
                label: "memory",
                data: [[0, 0]]
            }]);
        }
        if (($("#adminSystemStatusNetworkGraph").width() !== 0) && ($("#adminSystemStatusNetworkGraph").height() !== 0)) {
            $.plot($("#adminSystemStatusNetworkGraph"), [{
                label: "network_in",
                data: [[0, 0]]
            }]);
        }
        /*if (($("#adminSystemStatusDiskGraph").width() != 0) && ($("#adminSystemStatusDiskGraph").height() != 0)) {$.plot($("#adminSystemStatusDiskGraph"), [{
            label: "network_out",
            data: [[0, 0]]
        }]);}*/
        var selectedHost = $('td', fnGetSelected(adminSystemStatusHostTable));
        var selected;
        var siteName;
        siteName = $(selectedHost[0]).text();
        selected = selectedHost[1];
        monitorid = $(selected).text();
        if (time == '15sec') {
            time = 15000;
        }
        if (time == '1min') {
            time = 60000;
        }
        if (time == '5min') {
            time = 300000;
        }
        var pretime = -1500;
        getHostCpuInfoHistory(siteName, monitorid, type, pretime, drawHostCpuHistory);
        getMemInfoHistory(siteName, monitorid, type, pretime, drawMemHistory);
        getNetInInfoHistory(siteName, monitorid, type, pretime, drawNetInHistory);
        //getNetOutInfoHistory(siteName,monitorid,type,pretime,drawNetOutHistory);
        function fetchData() {
            getHostCpuInfoStatus(siteName, monitorid, type, cpu_all, dom0_cpu_user, dom0_cpu_system, dom0_cpu_iowait, drawHostCpuHistory);
            getMemInfoStatus(siteName, monitorid, type, memory, drawMemHistory);
            getNetInInfoStatus(siteName, monitorid, type, network_in, network_out, drawNetInHistory);
            //getNetOutInfoStatus(siteName,monitorid,type,network_out,drawNetOutHistory);
        }
        hostMonitorStatus = setInterval(fetchData, time);
    };

    $("#adminSystemStatusGraphIsRealtimeInput").click(function(event) {
        var checked = $(this)[0].checked;
        $("#adminSytemStatusGraphRealtime")[0].disabled = !checked;
        $("#adminSytemStatusGraphHistory")[0].disabled = checked;
    });
    $("#adminSystemStatusDrawGraphButton").click(function(event) {
        var selectedHost = $('td', fnGetSelected(adminSystemStatusHostTable));
        var selectedVM = $('td', fnGetSelected(adminSystemStatusVMTable));
        var selected;
        if ($("#adminSystemStatusVMTableTab_").hasClass('ui-tabs-selected')) {
            if (selectedVM.length === 0) {
                alert("please select one row!");
                return 0;
            }
        }
        if ($("#adminSystemStatusHostTableTab_").hasClass('ui-tabs-selected')) {
            if (selectedHost.length === 0) {
                alert("please select one row!");
                return 0;
            }
        }
        type = $("#adminSytemStatusGraphType")[0].value;
        time = $("#adminSytemStatusGraphRealtime")[0].value;
        periods = $("#adminSytemStatusGraphHistory")[0].value;
        if ($("#adminSytemStatusGraphHistory")[0].disabled === true) {
            if ($("#adminSystemStatusVMTableTab_").hasClass('ui-tabs-selected')) {
                $('#bottomInfoText').text( $(selectedVM[1]).text() + ':' + $(selectedVM[0]).text());
                clearInterval(vmMonitorStatus);
                clearInterval(hostMonitorStatus);
                plotAdminSystemVMStatusGraph();
            }
            else {
                $('#bottomInfoText').text( $(selectedHost[0]).text() + ':' + $(selectedHost[1]).text());
                clearInterval(vmMonitorStatus);
                clearInterval(hostMonitorStatus);
                plotAdminSystemHostStatusGraph();
            }
        }
        else {
            if ($("#adminSystemStatusVMTableTab_").hasClass('ui-tabs-selected')) {
                $('#bottomInfoText').text( $(selectedVM[1]).text() + ':' + $(selectedVM[0]).text());
                clearInterval(vmMonitorStatus);
                clearInterval(hostMonitorStatus);
                plotAdminSystemVMStatusGraphHistory();
            }
            else {
                $('#bottomInfoText').text( $(selectedHost[0]).text() + ':' + $(selectedHost[1]).text());
                clearInterval(vmMonitorStatus);
                clearInterval(hostMonitorStatus);
                plotAdminSystemHostStatusGraphHistory();
            }
        }
    });
    $("#adminSystemStatusRefreshTableButton").click(function(event) {
        getAdminMonitorHosts(loadadminMonitorHosts);
        getAdminMonitorVMs(loadadminMonitorVMs);
    });
    getAdminMonitorHosts(loadadminMonitorHosts);
    getAdminMonitorVMs(loadadminMonitorVMs);
    //});
};
